var textArray = new Array("仅自己可见","仅好友可见","同网络及好友可见","所有人可见");
var flag = true;
$(document).ready(function(){
	$(".contactSetting").each(function(){
		if($(this).prev().val()!=""){
			$(this).html(textArray[$(this).html()]);
			$(this).bind("click",function(){setContactSetting(this);});
			$(this).mouseover(function(){  
				$(this).addClass("over");}).mouseout(function(){ 
                 	$(this).removeClass("over");});
		}else{
			$(this).remove();
		}
		//alert($(this).prev().val());
	});
	
	$(".contactinput").each(function(){
		$(this).bind("blur",function(){setContactInput(this);});
	});
	
	//setContactSetting(obj);
});

function setContactSetting(obj){
	//$('.contactSetting').click(function(){
	if(flag){
		flag = false;
		$(obj).mouseover(function(){  
				$(obj).removeClass("over");});
		var input = ' <select class="contactSelect">';
		for(var i=0 ; i<textArray.length ; i++){
			input += '<option value="'+i+'" ';
			if(textArray[i] == $(obj).html()){
				input += "selected";
			}
			input += '>'+textArray[i]+'</option>';			
		}
		input += '</select>';
		$(obj).html(input); 		
		$('.contactSelect').focus();
		var sv = $('.contactSelect').val()
		$('.contactSelect').blur(function(){
			setContactSelect(this,sv);
		})
	}
	//});
}

function setContactSelect(obj,v){
	flag = true;
	var pObj = $(obj).parent();
	if( v != $(obj).val()){
		$.post(APP+"/ajax/savecontactsetting", {field: $(pObj).prev().attr("name"), value: $(obj).val() } );
		msg(pObj,"设置成功");
	}
	$(pObj).html(textArray[$(obj).val()]);
	$(pObj).bind("click",function(){setContactSetting(this);});
	$(pObj).mouseover(function(){  
				$(pObj).addClass("over");}).mouseout(function(){ 
                 	$(pObj).removeClass("over");});
	//alert($(pObj).prev().attr("name"));
}

function setContactInput(obj){
	if($.trim($(obj).val())!="" && $(obj).next().html()==null){
		var input = '<span class="contactSetting">'+textArray[1]+'</span>';
		$(obj).after(input);
		flag = true;
		$(obj).next().bind("click",function(){setContactSetting(this);});
		$(obj).next().mouseover(function(){  
				$(this).addClass("over");}).mouseout(function(){ 
                 	$(this).removeClass("over");});
		$(obj).next().click();
	}
	
	if($.trim($(obj).val())=="" && $(obj).next().html()!=null){
		$(obj).next().remove();
	}
}